﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TBFramework;
using System;
using DG.Tweening;
namespace UIController
{
    public class CarouselImageComponent : UIParentClass
    {

        // 图片按钮
        int isClickDis;
        // 图片列表
        public Sprite[] t2d;
        // 图片显示组件
        public Image[] rawImage;
        // 轮播图按钮
        public Button[] buttons;
        // 当前页码
        int currentPageNum;
        // 是否正在点击
        bool isButtonClick;
        // 轮播图翻页数
        int turningPageNum;

        Action<bool> action;
        public override void Init<T>(T values, Action<bool> _action)
        {
            for (int i = 0; i < rawImage.Length; i++)
            {
                rawImage[i].transform.localRotation = Quaternion.Euler(0, 0, 0);
                rawImage[i].transform.SetSiblingIndex(rawImage.Length - i - 1);
            }
            action = _action;
            t2d = values as Sprite[];
            rawImage[0].sprite = t2d[0];
            isButtonClick = false;
            turningPageNum = 0;
            currentPageNum = 0;
            isClickDis = -1;
            buttons[2].gameObject.SetActive(false);
        }

        public void Previous()
        {
            if (!isButtonClick)
            {
                if (currentPageNum > 0)
                {
                    if (isClickDis == 1)
                    {
                        currentPageNum--;
                        turningPageNum--;
                        for (int i = 0; i < rawImage.Length; i++)
                        {
                            if (i != Math.Abs(turningPageNum + 1) % 3)
                            {
                                rawImage[i].transform.localRotation = Quaternion.Euler(0, 120, 0);
                            }
                            else
                            {
                                rawImage[Math.Abs(turningPageNum) % 3].transform.SetSiblingIndex(2);
                            }
                        }
                    }
                    isClickDis = 2;
                    rawImage[Math.Abs(turningPageNum % 3)].sprite = t2d[currentPageNum];
                    rawImage[Math.Abs(turningPageNum - 1) % 3].sprite = t2d[currentPageNum - 1];
                    currentPageNum--;
                    isButtonClick = true;
                    rawImage[turningPageNum % 3].transform.DOLocalRotate(new Vector3(0, 0, 0), 0.5f).OnComplete(() =>
                    {
                        for (int i = 0; i < rawImage.Length; i++)
                        {
                            if (i != Math.Abs(turningPageNum + 1) % 3)
                            {
                                rawImage[i].transform.localRotation = Quaternion.Euler(0, 120, 0);
                            }
                            else
                            {
                                rawImage[Math.Abs(turningPageNum) % 3].transform.SetSiblingIndex(2);
                            }
                        }
                        isButtonClick = false;
                    });
                    turningPageNum--;
                }
                else if (currentPageNum == 0)
                {
                    isButtonClick = true;
                    turningPageNum = 0;
                    isClickDis = -1;
                    rawImage[1].transform.localRotation = Quaternion.Euler(0, 0, 0);
                    rawImage[2].transform.localRotation = Quaternion.Euler(0, 0, 0);
                    rawImage[1].transform.SetSiblingIndex(1);
                    rawImage[2].transform.SetSiblingIndex(0);
                    rawImage[0].transform.DOLocalRotate(new Vector3(0, 0, 0), 0.5f).OnComplete(() =>
                    {
                        rawImage[0].transform.SetSiblingIndex(2);
                        isButtonClick = false;
                    });
                }
            }
        }
        public void Next()
        {
            if (!isButtonClick)
            {
                if (currentPageNum < t2d.Length - 1)
                {
                    if (isClickDis == 2)
                    {
                        currentPageNum++;
                        turningPageNum++;

                        for (int i = 0; i < rawImage.Length; i++)
                        {
                            if (i != (turningPageNum - 1) % 3)
                            {
                                rawImage[i].transform.localRotation = Quaternion.Euler(0, 0, 0);
                            }
                            else
                            {
                                rawImage[turningPageNum % 3].transform.SetSiblingIndex(2);
                            }
                        }
                    }
                    isClickDis = 1;
                    rawImage[turningPageNum % 3].sprite = t2d[currentPageNum];
                    rawImage[(turningPageNum + 1) % 3].sprite = t2d[currentPageNum + 1];
                    currentPageNum++;
                    isButtonClick = true;
                    rawImage[turningPageNum % 3].transform.DOLocalRotate(new Vector3(0, 120, 0), 0.5f).OnComplete(() =>
                    {
                        for (int i = 0; i < rawImage.Length; i++)
                        {
                            if (i != (turningPageNum - 1) % 3)
                            {
                                rawImage[i].transform.localRotation = Quaternion.Euler(0, 0, 0);
                            }
                            else
                            {
                                rawImage[turningPageNum % 3].transform.SetSiblingIndex(2);
                            }
                        }
                        isButtonClick = false;
                        if (currentPageNum == t2d.Length - 1)
                        {
                            buttons[2].gameObject.SetActive(true);
                        }
                    });
                    turningPageNum++;
                }
            }
        }
        public void OnMouseOver()
        {
            action?.Invoke(true);
        }

    }
}
